﻿//////////////////////////////////"tablelist"//////////////////////////
//datable对象
var hcmtable;
var tablelistSelectType = 1;//1行多选 0行单选   -1不处理
$(function () {
    //行选择
    if (tablelistSelectType == 1) {
        $('#tablelist tbody').on('click', 'tr', function () {
            $(this).toggleClass('selected');
        });
    }
    else if (tablelistSelectType == 0) {
        //单选
        $('#tablelist tbody').on('click', 'tr', function () {
            if ($(this).hasClass('selected')) {
                $(this).removeClass('selected');
            }
            else {
                hcmtable.$('tr.selected').removeClass('selected');
                $(this).addClass('selected');
            }
        });
    }
})

//弹给主窗体的信息--用于子窗体信息传递
var hcdivbackinfo = "no";

//全选与反选
function hcmtable_sall() {
    if ($("#hcmtable_sall").html().indexOf("iiselctyes") < 0) {
        $("#hcmtable_sall").html('<i class="Hui-iconfont" id="iiselctyes">&#xe676;全</i>');
    }
    else { $("#hcmtable_sall").html('<i class="Hui-iconfont">&#xe608;全</i>'); }
    hcmtable.$('tr').each(function () {
        if ($(this).hasClass('selected')) {
            $(this).removeClass('selected');
        }
        else {
            $(this).addClass('selected');
        }
    });
}
//清空选择
function hcmtable_sno() {
    if (hcmtable)
        hcmtable.$('tr.selected').removeClass('selected');
}


//$.fn.dataTableExt.oApi.fnReloadAjax = function (oSettings) {
//    //oSettings.sAjaxSource = sNewSource;
//    this.fnClearTable(this);
//    this.oApi._fnProcessingDisplay(oSettings, true);
//    var that = this;

//    $.getJSON(oSettings.sAjaxSource, null, function (json) {
//        /* Got the data - add it to the table */
//        for (var i = 0; i < json.aaData.length; i++) {
//            that.oApi._fnAddData(oSettings, json.aaData[i]);
//        }
//        oSettings.aiDisplay = oSettings.aiDisplayMaster.slice();
//        that.fnDraw(that);
//        that.oApi._fnProcessingDisplay(oSettings, false);
//    });
//}


//重新加载
function findreolad(isback) {
    if (hcmtable) {
        //var oTable = $('#tablelist').dataTable();
        //var dfdfd = oTable.fnSettings();
        if (isback && isback == "sdivclosed")
            hcmtable.ajax.reload(null, false);
        else
            hcmtable.ajax.reload();
        // window.location.reload();
        // oTable.fnReloadAjax(fnSettings);
        //oTable.fnReloadAjax(oTable.fnSettings());
        // hcmtable.fnDraw();
        // hcmtable.ajax.url().load()
        //  hcmtable.ajax.reload();
        //  hcmtable.ajax.reload();
    }
}





//通用标准的toolUpdate
function toolUpdate() {
    var ID = "";
    var rowdata = hcmtable.rows('.selected').data();
    var slen = rowdata.length;
    if (slen < 1) {
        layer.alert('请选择要修改的内容');
        return;
    }
    //if (slen > 1) { layer.alert('选择了多个，将处理第一个!'); }
    for (var i = 0; i < slen; i++) {
        ID = rowdata[i][pTableIdName];
        break;
    }
    toolUpdateById(ID);
}
//分析要打开的url
function toolUpdateByIdCheckUrl(id, url, tinfo) {
    if (url != null && url != '') {
        url = url + "?action=edit&id=" + id;
    };
    var t = null;
    if (tinfo != 'undefined' && tinfo != null && tinfo != '') {
        t = tinfo;
    }
    toolUpdateById(id, t, url, null, null);
}
//根据ID修改
function toolUpdateById(id, t, url, w, h) {
    if (url == null || url == '') {
        url = spPageHtmlName + "-ae.html?action=edit&id=" + id;
    };
    if (t == null || t == '') {
        t = "处理" + pPageInfoName + "信息";
    };

    hcmtable_sno();
    if (w == null || w == '') {
        var index = layer.open({
            type: 2,
            title: t,
            content: url,
            end: function () {
                findreolad("sdivclosed");//子窗体已关
            }
        });
        layer.full(index);
    }
    else {
        if (w == null || w == '') {
            w = 800;
        };
        if (h == null || h == '') {
            h = ($(window).height() - 50);
        };
        layer.open({
            type: 2,
            area: [w + 'px', h + 'px'],
            fix: false,
            maxmin: true,
            shade: 0.4,
            title: t,
            content: url,
            end: function () {
                findreolad("sdivclosed");
            }
        });
    }
}

//通用标准的toolDelete  inids:可指定ID，或者根据行选择处理
function toolDelete(inids, tname, tid) {
    var del = 1;
    var ID = "";
    if (inids) {
        hcmtable_sno();
        ID = inids;
    }
    else {
        var rowdata = hcmtable.rows('.selected').data();
        del = rowdata.length;
        for (var i = 0; i < del; i++) {
            ID += rowdata[i][pTableIdName] + ",";
        }
        ID = ID.substring(0, ID.length - 1);
    }
    if (ID.length == 0) {
        layer.alert('请选择要删除的内容');
        return;
    }
    var delurl;
    if (tname == null || tname == '') {
        delurl = spdelUrl(ID);
    }
    else {
        delurl = spodelUrl(tname, tid, ID);
    }
    layer.confirm("确认要删除吗？记录数：" + del, function (index) {
        var indexer = layer.load(2, {
            shade: [0.1, '#fff'] //0.1透明度的白色背景
        });

        $.get(delurl,
         function (data) {
             layer.close(indexer);
             var data = JSON.parse(data);
             switch (data.message) {
                 case JsonResult.SessionOut:
                     layer.msg('登录超时，请重新登录!', { icon: 2, time: 1000 });
                     break;
                 case JsonResult.Error:
                     layer.msg('系统出错!' + data.data, { icon: 2, time: 1000 });
                     break;
                 case JsonResult.Success:
                     layer.msg('已删除!', { icon: 1, time: 1000 });
                     findreolad();
                     break;
                 default:
                     layer.msg('删除失败!' + data.message, { icon: 2, time: 1000 });
                     break;
             }
         });

    });
}
var toolselectObj = [];
function toolSelect(pagetype) {
    var ID = "";
    var rowdata = hcmtable.rows('.selected').data();
    del = rowdata.length;
    for (var i = 0; i < rowdata.length; i++) {
        var hascheck = false;
        for (var j = 0; j < toolselectObj.length; j++) {
            if (toolselectObj[j]["id"] == rowdata[i][pTableIdName]) {
                hascheck = true; break;

            }
        }
        if (!hascheck) {
            toolselectObj.push({
                "id": rowdata[i]["id"], "name": rowdata[i]["name"]
            });
        }
    }
    var infoshowtext = "";
    for (var j = 0; j < toolselectObj.length; j++) {
        infoshowtext = infoshowtext + toolselectObj[j].name + "/";
    }
    parent.settoolselectObj(toolselectObj, pagetype);
    $("#toolselectinfoshow").html(infoshowtext);
    $("#toolselectinfoshowcount").html(toolselectObj.length);
}
function toolSelectCleanOne(pagetype) {
    var ID = "";
    var rowdata = hcmtable.rows('.selected').data();
    del = rowdata.length;
    for (var i = 0; i < rowdata.length; i++) {
        var hascheck = false;
        for (var j = 0; j < toolselectObj.length; j++) {
            if (toolselectObj[j]["id"] == rowdata[i][pTableIdName]) {
                hascheck = true;
                toolselectObj.splice(j, 1);
                break;

            }
        }
    }

    var infoshowtext = "";
    for (var j = 0; j < toolselectObj.length; j++) {
        infoshowtext = infoshowtext + toolselectObj[j].name + "/";
    }
    parent.settoolselectObj(toolselectObj, pagetype);
    $("#toolselectinfoshow").html(infoshowtext);
    $("#toolselectinfoshowcount").html(toolselectObj.length);
}
function toolSelectClean() {
    toolselectObj = [];
    $("#toolselectinfoshow").html("");
}
//通用标准的toolAdd 
function toolAdd(t, url, w, h) {
    if (url == null || url == '') {
        url = spPageHtmlName + "-ae.html?action=add";
    };
    if (t == null || t == '') {
        t = "新增" + pPageInfoName + "信息";
    };

    if (w == null || w == '' || w == 'undefined') {
        var index = layer.open({
            type: 2,
            title: t,
            content: url,
            //cancel: function (index) {//关闭
            //    layer.close(index);
            //},
            //success: function (layero, index) {
            //    console.log(layero, index);
            //},
            //yes: function (index) {
            //    //do something
            //    layer.close(index); //一般设定yes回调，必须进行手工关闭
            //},
            end: function () {
                findreolad("sdivclosed");
            }
        });
        layer.full(index);
    }
    else {
        if (w == null || w == '') {
            w = 800;
        };
        if (h == null || h == '') {
            h = ($(window).height() - 50);
        };
        layer.open({
            type: 2,
            area: [w + 'px', h + 'px'],
            fix: false,
            maxmin: true,
            shade: 0.4,
            title: t,
            content: url,
            end: function () {
                findreolad("sdivclosed");
            }
        });

    }



}


//通用标准的toolAdd 
function toolAddMain(t, url, w, h) {
    if (url == null || url == '') {
        url = spPageHtmlName + "-ae.html?action=add";
    };
    if (t == null || t == '') {
        t = "新增" + pPageInfoName + "信息";
    };

    if (w == null || w == '' || w == 'undefined') {
        var index = parent.layer.open({
            type: 2,
            title: t,
            content: url,
            //cancel: function (index) {//关闭
            //    layer.close(index);
            //},
            //success: function (layero, index) {
            //    console.log(layero, index);
            //},
            //yes: function (index) {
            //    //do something
            //    layer.close(index); //一般设定yes回调，必须进行手工关闭
            //},
            end: function () {
                findreolad("sdivclosed");
            }
        });
        parent.layer.full(index);
    }
    else {
        if (w == null || w == '') {
            w = 800;
        };
        if (h == null || h == '') {
            h = ($(window).height() - 50);
        };
        layer.open({
            type: 2,
            area: [w + 'px', h + 'px'],
            fix: false,
            maxmin: true,
            shade: 0.4,
            title: t,
            content: url,
            end: function () {
                findreolad("sdivclosed");
            }
        });

    }
}


function opensimplediv(t, url, w, h) {
    if (url == null || url == '') {
        url = spPageHtmlName + "-ae.html?action=add";
    };
    if (t == null || t == '') {
        t = "新增" + pPageInfoName + "信息";
    };



    if (w == null || w == '') {
        w = 420;
    };
    if (h == null || h == '') {
        h = 240;
    };
    layer.open({
        type: 2,
        area: [w + 'px', h + 'px'],
        skin: 'layui-layer-rim', //加上边框
        fix: false,
        maxmin: true,
        shade: 0.4,
        title: t,
        content: url,
        end: function () {
            findreolad("sdivclosed");
        }
    });

}

//翻页
function dTappendSkipPage() {
    var table = $("#tablelist").dataTable();
    var template =
        $("<li class='paginate_buttonhc'>" +
            "   <div class='input-group' style='margin-left:3px;'>" +
            "       <span  style='padding:1px 8px;background-color:#fff;font-size: 12px;'>跳转</span>" +
            "       <input type='text' class='input-text' style='margin-top:-3px;width: 26px;height:22px;' />" +
            "       <span  style='padding:1px 8px;'><a href='javascript:void(0)'> Go </a></span>" +
            "   </div>" +
            "</li>");

    template.find("a").click(function () {
        var pn = template.find("input").val();
        if (pn > 0) {
            --pn;
        } else {
            pn = 0;
        }
        table.fnPageChange(pn);
    });
    $("#tablelist_paginate").append(template);
}





////通用标准的toolUpdate  itemEditContainer---edititemblock--itemEdit
//function toolUpdate() {

//    var row = $(grid1ID).datagrid("getSelections");
//    if (row.length == 0) {
//        $.messager.alert("提示信息", "请选择需要修改的数据！"); return;
//    }
//    var id = row[0][pTableIdName];
//    toolUpdateById(id);
//}
////根据行号
//function toolUpdateForId(index) {
//    $(grid1ID).datagrid("selectRow", index);
//    var row = $(grid1ID).datagrid("getSelected");
//    var id = row[pTableIdName];
//    toolUpdateById(id);
//}